$(document).ready(function () {

    $('#data-wrapper select').select0();

    /*查询按钮事件*/
    $('#data-wrapper .btn-4-query').off('click').on('click', function () {
        ajaxSubmitResponseHtm({
            url: 'manager/query.htm',
            method: 'POST',
            data: $('#data-wrapper form').serialize()
        });
    });

    /*新增用户按钮事件*/
    $('#data-wrapper .btn-4-add').off('click').on('click', function () {
        ajaxSubmitResponseHtm({
            container: '#pop-wrapper',
            url: 'manager/edit.htm'
        });
    });

    /*修改用户按钮事件*/
    $('#data-wrapper .btn-4-modify').off('click').on('click', function () {
        var id = $(this).data('id');
        if (id) {
            ajaxSubmitResponseHtm({
                container: '#pop-wrapper',
                url: 'manager/edit.htm',
                data: 'id=' + id
            });
        }
    });

    $('#data-wrapper .btn-4-reset-password').off('click').on('click', function () {
        var id = $(this).data('id');
        if (id) {
            layer.confirm('确认重置用户密码为初始密码？',
                {icon: 3, title: '提示', btn: ['确认', '取消']},
                function () {
                    ajaxSubmitResponseJSON({
                        url: 'manager/resetPassword.htm/' + id
                    });
                }
            );
        }
    });

    $('#data-wrapper .btn-4-delete').off('click').on('click', function () {
        var id = $(this).data('id');
        if (id) {
            layer.confirm('确认删除？',
                {icon: 3, title: '删除提示', btn: ['确认', '取消']},
                function () {
                    ajaxSubmitResponseJSON({
                        url: 'manager/delete.htm/' + id,
                        success: function (data) {
                            if (data.status == 0) {
                                $('#data-wrapper .btn-4-query').trigger('click');
                            }
                        }
                    });
                }
            );
        }
    });

    $('#data-wrapper .btn-4-enable').off('click').on('click', function () {
        var id = $(this).data('id');
        if (id) {
            ajaxSubmitResponseJSON({
                url: 'manager/enable.htm/' + id,
                success: function (data) {
                    if (data.status == 0) {
                        $('#data-wrapper .btn-4-query').trigger('click');
                    }
                }
            });
        }
    });

    $('#data-wrapper .btn-4-disable').off('click').on('click', function () {
        var id = $(this).data('id');
        if (id) {
            layer.confirm('确认锁定？',
                {icon: 3, title: '锁定提示', btn: ['确认', '取消']},
                function () {
                    ajaxSubmitResponseJSON({
                        url: 'manager/disable.htm/' + id,
                        success: function (data) {
                            if (data.status == 0) {
                                $('#data-wrapper .btn-4-query').trigger('click');
                            }
                        }
                    });
                }
            );
        }
    });

    /*pageSize变化，从新加载数据*/
    $('#data-wrapper .paging-size-List').off('change').on('change', function () {
        var length = $(this).val();
        $('#data-wrapper form input[name=length]').val(length);
        $('#data-wrapper .btn-4-query').trigger('click');
    });

    /*点击分页按钮，实现翻页功能*/
    $('#data-wrapper .pagination-toolbar a').off('click').on('click', function () {
        var distStart = $(this).data('idx');
        var start = $('#data-wrapper form input[name=start]').val();
        if (start != distStart) {
            $('#data-wrapper form input[name=start]').val(distStart);
            $('#data-wrapper .btn-4-query').trigger('click');
        }
    });

});